package com.zlt.miaosha.service;

import com.zlt.miaosha.dao.GoodsDao;
import com.zlt.miaosha.dao.MiaoshaUserDao;
import com.zlt.miaosha.domain.Goods;
import com.zlt.miaosha.domain.MiaoshaGoods;
import com.zlt.miaosha.domain.MiaoshaUser;
import com.zlt.miaosha.exception.GlobalException;
import com.zlt.miaosha.redis.MiaoshaUserKey;
import com.zlt.miaosha.redis.RedisService;
import com.zlt.miaosha.result.CodeMsg;
import com.zlt.miaosha.util.MD5Util;
import com.zlt.miaosha.util.UUIDUtil;
import com.zlt.miaosha.vo.GoodsVo;
import com.zlt.miaosha.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zhoulitong
 * @date 2018/6/13
 * @description
 */
@Service
public class GoodsService {

    @Autowired
    GoodsDao goodsDao;

    public List<GoodsVo> listGoodsVo(){
        return goodsDao.listGoodsVo();
    }

    public GoodsVo getGoodsVoByGoodsId(long goodsId) {
        return goodsDao.getGoodsVoByGoodsId(goodsId);
    }

    @Transactional
    public boolean reduceStock(GoodsVo goods) {
        MiaoshaGoods g = new MiaoshaGoods();
        g.setGoodsId(goods.getId());
        int ret = goodsDao.reduceStock(g);
        return ret>0;
    }

    public void resetStock(List<GoodsVo> goodsList) {
        for(GoodsVo goods : goodsList ) {
            MiaoshaGoods g = new MiaoshaGoods();
            g.setGoodsId(goods.getId());
            g.setStockCount(goods.getStockCount());
            goodsDao.resetStock(g);
        }
    }
}
